<?php

namespace app\thirdPay\logic;

use app\data\util\HttpUtil;
use app\order\model\Order as OrderModel;
use think\facade\Config;
use think\facade\Db;
use function app\order\logic\dump;
use function get_pay_id;
use function json;

class AirwallexServiceLogic
{

    protected $model = null;
    protected $config = [];

    public function __construct()
    {
        $this->model = new OrderModel();
        $this->config  = Config::get('Logisticskey.' . 'Airwallex');

        //token存表  
        $yunhui_token = Db::name('yunhui_token')->where('id',1)->find();
        $this->config['token'] = $yunhui_token['token'];
    }

    /**
     * 获取空中云汇回款
     * @param $type
     * @return mixed
     */
    public function GetPaymentsAirwallexToken($params=[])
    {
        //token
        $url = "https://api.airwallex.com/api/v1/authentication/login";
        $return = HttpUtil::posturlAirwallex($url, $this->config['id'], $this->config['apikey']);
        $return = json_decode($return, true);

        //token存表
        $update['token'] = $return['token'];   
        Db::name('yunhui_token')->where('id',1)->update($update);

        return json(DefaultResponse(200, true, '操作成功!'));
    }

    /**
     * 获取空中云汇回款
     * @param $type
     * @return mixed
     */
    public function GetPaymentsAirwallex($params=[])
    {
        //类型1 获取历史数据
        // $currentMonth = date('m');
        // $currentYear  = date('Y');
        // if ($currentMonth == 1) {  
        //     $lastMonth = 12; // 如果是1月，则上个月是12月  
        //     $lastYear  = $currentYear - 1; // 上一年  
        // } else {  
        //     $lastMonth = $currentMonth - 1; // 否则，上个月是当前月份减一  
        //     $lastYear  = $currentYear; // 年份不变  
        // } 
        // $times = get_month_time($lastYear,$lastMonth);
        // $from_created_at = date('Y-m-d',$times['first']);
        // $to_created_at = date('Y-m-d',$times['last']);

        //类型2 每天获取实时数据
        $from_created_at = date('Y-m-d',time() - 3600*24*2);
        $to_created_at = date('Y-m-d',time());
        //存款清单
        $url = "https://api.airwallex.com/api/v1/financial_transactions?currency=USD&from_created_at=".$from_created_at."&page_num=0&page_size=1000&to_created_at=".$to_created_at;
        $return = HttpUtil::geturlAirwallex($url,$this->config['token']);
        $return = json_decode($return, true);
        // dump($return);die;
        if(isset($return['items']) && !empty($return['items'])){
            $airwallex = $return['items'];
            foreach ($airwallex as $value) {
                $pay_id = get_pay_id($value['description']);
                $shop_id = 0;
                if(!empty($pay_id)){
                    $bank_pay_info = Db::name('bank_pay_info')->where('pay_id',$pay_id)->find();
                    $shop_id = empty($bank_pay_info['shop_id'])? '': $bank_pay_info['shop_id'];
                }
                $airwallex_list = [
                    'shop_id' => empty($shop_id)? 0: $shop_id,
                    'pay_id' => empty($pay_id)? 0: $pay_id,
                    'amount' => $value['amount'],
                    'source_id' => $value['sourceId'],
                    'currency' => $value['currency'],
                    'status' => $value['status'],
                    'created_at' => strtotime($value['createdAt']),
                    'date'=>date('Y-m', strtotime($value['createdAt'])),
                ];
                $airwallex = Db::name('bank_pay_yunhui')->where('source_id',$airwallex_list['source_id'])->find();
                if(!empty($airwallex)){
                    Db::name('bank_pay_yunhui')->where('id',$airwallex['id'])->update($airwallex_list);
                }else{
                    Db::name('bank_pay_yunhui')->insert($airwallex_list);
                }
            }
        }
        return json(DefaultResponse(200, true, '操作成功!'));
    }

    /**
     * 测试
     * @param $type
     * @return mixed
     */
    public function AirwallexTest($params=[])
    {
        $from_created_at = "2024-04-01";
        $to_created_at = "2024-04-03";
        //获取金融交易列表
        $url = "https://api.airwallex.com/api/v1/financial_transactions?currency=USD&from_created_at=".$from_created_at."&page_num=0&page_size=100&to_created_at=".$to_created_at;
        //回款记录
        // $url = "https://api.airwallex.com/api/v1/deposits?from_created_at=".$from_created_at."&page_num=0&page_size=100&to_created_at=".$to_created_at;

        //全球账户列表
        $url = "https://api.airwallex.com/api/v1/global_accounts";
        $url = "https://api.airwallex.com/api/v1/global_accounts/38a93f04-fb14-46bb-aff8-e20a6db2c6af/mandates/1";
        $url = "https://api.airwallex.com/api/v1/global_accounts/38a93f04-fb14-46bb-aff8-e20a6db2c6af/mandates";
        //全球结算账户按id查找
        $url = "https://api.airwallex.com/api/v1/global_accounts/38a93f04-fb14-46bb-aff8-e20a6db2c6af/transactions?page_num=0&page_size=100";
        //获取金融交易列表
        $url = "https://api.airwallex.com/api/v1/financial_transactions?currency=USD&from_created_at=".$from_created_at."&page_num=0&page_size=100&to_created_at=".$to_created_at;

        $return = HttpUtil::geturlAirwallex($url,$this->config['token']);
        dump($return);die;
        
    }
    
}

